package com.health.fragment.focusgroup;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.health.NovaApplication;
import com.health.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.focusgroup.GXBActivity;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_CHD_FOLLOWUP;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;
import com.health.widget.MyGridView;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.ExamHistoryActivity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 重点人群-- 冠心病---随访
 */
public class GXB_SfFragment extends NovaFragment implements onFragmentSave, OnClickListener {
	private static final String TAG="GXB_SfFragment";
	private View cqsf2View = null;
	private FDbManager fdbmanger = null;

	// ------------------------------基本信息-----------------------------
	private EditText ed_xm;

	private EditTextWithDate ed_date;
	private NiceSpinner ed_sfys;// 随访医生
	private NiceSpinner nice_spinner_szfs;// 选择随诊方式

	private MyGridView gd_zz;// 症状

	private EditText ed_xy;// 血压
	private Button btn_choose_xy;

	private EditText ed_tz;// 体重
	private Button btn_choose_tz;// 选择体重
	private MyGridView gd_dyxysfjr;// 第一心音是否减弱：

	private MyGridView gd_xlsc;// 心律失常是否闻及早搏心房纤颤：
	private MyGridView gd_xjbsfwj;// 心肩部是否闻及奔马律：
	private MyGridView gd_zzgy;// 症状出现后服硝酸\n甘油后是否缓解:

	private EditText ed_yj_zysx;// 烟酒注意事项
	private EditText ed_yd_zysx;// 运动注意事项
	private EditText ed_sy_zysx;// 摄盐注意事项

	private NiceSpinner nice_spinner_zyxw;// 遵医行为

	private EditText ed_fzjc;// 辅助检查
	private NiceSpinner nice_spinner_fyycx;// 服药依从性
	private MyGridView gd_ywblfy;// 药物不良反应

	private NiceSpinner nice_spinner_clsffl;// 此类随访分类

	private EditText ed_yw1;// 药物名称1

	private EditText ed_yw1_mr;// 每日 day
	private EditText ed_yw1_mcjl;// 每次 mg

	private EditText ed_yw2;// 药物名称1

	private EditText ed_yw2_mr;// 每日 day
	private EditText ed_yw2_mcjl;// 每次 mg

	private EditText ed_yw3;// 药物名称1

	private EditText ed_yw3_mr;// 每日 day
	private EditText ed_yw3_mcjl;// 每次 mg

	private EditText ed_yw4;// 药物名称1

	private EditText ed_yw4_mr;// 每日 day
	private EditText ed_yw4_mcjl;// 每次 mg

	private EditText ed_zzyy;// 转诊原因
	private EditText ed_jgkb;// 机构及科别
	private EditTextWithDate ed_xcsfsj;// 下次随访时间
	private NiceSpinner nice_spinner_dw1;// 药物单位1
	private NiceSpinner nice_spinner_dw2;// 药物单位2
	private NiceSpinner nice_spinner_dw3;// 药物单位3
	private NiceSpinner nice_spinner_dwqt;// 药物单位其他
	// ----------------------------------------------------------------------
	private RadioButtonAdapter adapter_ywblfy = null;
	private RadioButtonAdapter adapter_dyxysfjr = null;
	private RadioButtonAdapter adapter_xlsc = null;
	private RadioButtonAdapter adapter_xjbsfwj = null;
	private RadioButtonAdapter adapter_zzgy = null;

	private CheckBoxAdapter adapter_zz = null;

	private List<BD_DD_INFO> szfs_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> zyxw_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> fyycx_list = new ArrayList<BD_DD_INFO>();

	private List<BD_DD_INFO> ccsffl_list = new ArrayList<BD_DD_INFO>();
	private List<BD_DD_INFO> ywdw_list = new ArrayList<BD_DD_INFO>();

	private String str_dyxyId = "";
	private String str_xlscId = "";
	private String str_xjbId = "";
	private String str_gyId = "";
	private String str_ywblfyContent = "";
	private String str_ywblfyId = "";
	private String str_zzContent = "";
	private String str_zzId = "";

	public ARCHIVE_BASEINFO userInfo;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	private int from_followup = -1;
	private int from_drug = -1;
	private int drug_id1 = -1;
	private int drug_id2 = -1;
	private int drug_id3 = -1;
	private int drug_id4 = -1;

	@Override
	public View findViews(LayoutInflater inflater) {
		fdbmanger = NovaApplication.fdbmanger;
		userInfo = NovaApplication.getUserInfo();
		cqsf2View = inflater.inflate(R.layout.gxb_sfxx, null);
		initViews(cqsf2View);
		GlobalConts.setMaxLength(13, ed_xy);
		GlobalConts.setMaxLength(6, ed_yw2_mcjl, ed_yw3_mcjl, ed_yw1_mcjl, ed_yw4_mcjl);
		GlobalConts.setMaxLength(7, ed_yj_zysx, ed_yd_zysx, ed_sy_zysx);
		GlobalConts.keep2decimal(ed_tz, ed_yw2_mcjl, ed_yw3_mcjl, ed_yw1_mcjl, ed_yw4_mcjl);
		GlobalConts.setMaxLength(3, ed_yw1_mr, ed_yw2_mr, ed_yw3_mr, ed_yw4_mr);
		GlobalConts.setMaxLength(25, ed_yw1, ed_yw2, ed_yw3, ed_yw4);
		GlobalConts.setMaxLength(50, ed_zzyy, ed_jgkb);// 机构及科别
		return cqsf2View;
	}

	/**
	 * 初始化布局
	 * 
	 * @param v
	 * @author 王婷婷
	 * @date 2015年12月21日 上午11:10:55
	 */
	private void initViews(View v) {

		ed_xm = (EditText) v.findViewById(R.id.ed_xm);
		ed_date = (EditTextWithDate) v.findViewById(R.id.ed_date);
		ed_sfys = (NiceSpinner) v.findViewById(R.id.ed_sfys);
		nice_spinner_szfs = (NiceSpinner) v.findViewById(R.id.nice_spinner_szfs);

		gd_zz = (MyGridView) v.findViewById(R.id.gd_zz);

		ed_xy = (EditText) v.findViewById(R.id.ed_xy);
		btn_choose_xy = (Button) v.findViewById(R.id.btn_choose_xy);

		ed_tz = (EditText) v.findViewById(R.id.ed_tz);
		btn_choose_tz = (Button) v.findViewById(R.id.btn_choose_tz);
		ed_yj_zysx = (EditText) v.findViewById(R.id.ed_yj_zysx);
		ed_yd_zysx = (EditText) v.findViewById(R.id.ed_yd_zysx);
		ed_sy_zysx = (EditText) v.findViewById(R.id.ed_sy_zysx);

		nice_spinner_zyxw = (NiceSpinner) v.findViewById(R.id.nice_spinner_zyxw);

		ed_fzjc = (EditText) v.findViewById(R.id.ed_fzjc);
		nice_spinner_fyycx = (NiceSpinner) v.findViewById(R.id.nice_spinner_fyycx);
		gd_ywblfy = (MyGridView) v.findViewById(R.id.gd_ywblfy);

		gd_dyxysfjr = (MyGridView) v.findViewById(R.id.gd_dyxysfjr);

		gd_xlsc = (MyGridView) v.findViewById(R.id.gd_xlsc);
		gd_xjbsfwj = (MyGridView) v.findViewById(R.id.gd_xjbsfwj);
		gd_zzgy = (MyGridView) v.findViewById(R.id.gd_zzgy);

		nice_spinner_clsffl = (NiceSpinner) v.findViewById(R.id.nice_spinner_clsffl);

		ed_yw1 = (EditText) v.findViewById(R.id.ed_yw1);
		ed_yw1_mr = (EditText) v.findViewById(R.id.ed_yw1_mr);

		ed_yw1_mcjl = (EditText) v.findViewById(R.id.ed_yw1_mcjl);

		ed_yw2 = (EditText) v.findViewById(R.id.ed_yw2);
		ed_yw2_mr = (EditText) v.findViewById(R.id.ed_yw2_mr);

		ed_yw2_mcjl = (EditText) v.findViewById(R.id.ed_yw2_mcjl);

		ed_yw3 = (EditText) v.findViewById(R.id.ed_yw3);
		ed_yw3_mr = (EditText) v.findViewById(R.id.ed_yw3_mr);

		ed_yw3_mcjl = (EditText) v.findViewById(R.id.ed_yw3_mcjl);

		ed_yw4 = (EditText) v.findViewById(R.id.ed_yw4);
		ed_yw4_mr = (EditText) v.findViewById(R.id.ed_yw4_mr);

		ed_yw4_mcjl = (EditText) v.findViewById(R.id.ed_yw4_mcjl);

		ed_zzyy = (EditText) v.findViewById(R.id.ed_zzyy);
		ed_jgkb = (EditText) v.findViewById(R.id.ed_jgkb);
		ed_xcsfsj = (EditTextWithDate) v.findViewById(R.id.ed_xcsfsj);
		nice_spinner_dw1 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw1);
		nice_spinner_dw2 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw2);
		nice_spinner_dw3 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw3);
		nice_spinner_dwqt = (NiceSpinner) v.findViewById(R.id.nice_spinner_dwqt);
		ed_date.setBefore(true);
		ed_xcsfsj.setAfter(true);
		GlobalConts.doctorList(ed_sfys);
	}

	/**
	 * 绑定数据值
	 * 
	 * @param str_where
	 * @param flag
	 * @author 王婷婷
	 * @date 2015年12月17日 上午10:30:30
	 */
	private void bindDdata(String flag, String str_where) {
		List<BD_DD_INFO> listZZs = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class, str_where);
		if (flag.equals("症状")) {
			adapter_zz = new CheckBoxAdapter(getActivity(), listZZs);
			adapter_zz.setOtherMaxLength(50);
			gd_zz.setAdapter(adapter_zz);
		} else if (flag.equals("药物不良反应")) {
			adapter_ywblfy = new RadioButtonAdapter(listZZs, getActivity());
			adapter_ywblfy.setOtherMaxLength(50);
			gd_ywblfy.setAdapter(adapter_ywblfy);

			adapter_dyxysfjr = new RadioButtonAdapter(listZZs, getActivity());
			adapter_dyxysfjr.setOtherMaxLength(50);
			adapter_dyxysfjr.setOtherTextVisible(false);
			gd_dyxysfjr.setAdapter(adapter_dyxysfjr);
			adapter_xlsc = new RadioButtonAdapter(listZZs, getActivity());
			adapter_xlsc.setOtherTextVisible(false);
			gd_xlsc.setAdapter(adapter_xlsc);

			adapter_xjbsfwj = new RadioButtonAdapter(listZZs, getActivity());
			adapter_xjbsfwj.setOtherMaxLength(50);
			adapter_xjbsfwj.setOtherTextVisible(false);
			gd_xjbsfwj.setAdapter(adapter_xjbsfwj);
			adapter_zzgy = new RadioButtonAdapter(listZZs, getActivity());
			adapter_zzgy.setOtherMaxLength(50);
			adapter_zzgy.setOtherTextVisible(false);
			gd_zzgy.setAdapter(adapter_zzgy);

		}
	}

	@Override
	public void setupView(View view) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addListener() {
		btn_choose_xy.setOnClickListener(this);
		btn_choose_tz.setOnClickListener(this);
	}

	@Override
	public void setData() {
		bindDdata("症状", "PARENTID='DM04-06'");
		bindDdata("药物不良反应", "PARENTID='DM01-11'");

		bindSpinner("随访方式", "PARENTID='DM04-04'");
		bindSpinner("遵医行为", "PARENTID='DM01-69'");
		bindSpinner("药物依从性", "PARENTID='DM01-70'");
		bindSpinner("此次随访分类", "PARENTID='DM01-71'");
		bindSpinner("药物单位", "PARENTID='DM04-05'");
		setData_followUP();
		setData_drug();

	}

	/**
	 * 设置下拉
	 * 
	 * @param flagName
	 * @param strSql
	 * @author 王婷婷
	 * @date 2015年12月24日 上午9:19:28
	 */

	private void bindSpinner(String flagName, String strSql) {
		List<BD_DD_INFO> sList = new ArrayList<BD_DD_INFO>();
		BD_DD_INFO bd = new BD_DD_INFO();
		bd.setDATAINFO("请选择");
		bd.setID("");
		sList.add(bd);

		List<BD_DD_INFO> sList1 = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class,
				strSql + " order by cast(SORT as int) asc");
		sList.addAll(sList1);
		if (flagName.equals("随访方式")) {
			// nice_adapter_pf = new NiceSpinnerAdapter<>(getActivity(), sList);
			// nice_spinner_pf.setAdapter(nice_adapter_pf);
			szfs_list = sList;
			nice_spinner_szfs.attachDataSource(szfs_list);

		} else if (flagName.equals("遵医行为")) {

			int size = sList.size();
			if (size > 0) {
				zyxw_list = new ArrayList<BD_DD_INFO>();
				for (int i = 0; i < size; i++) {
					String str_id = sList.get(i).getID();
					if (str_id.equals("DM01-69_1") || str_id.equals("DM01-69_2") || str_id.equals("DM01-69_3")
							|| str_id.equals("")) {
						zyxw_list.add(sList.get(i));
					}
				}
			}

			nice_spinner_zyxw.attachDataSource(zyxw_list);

		} else if (flagName.equals("药物依从性")) {
			// nice_adapter_lbj = new NiceSpinnerAdapter<>(getActivity(),
			// sList);
			fyycx_list = sList;
			nice_spinner_fyycx.attachDataSource(fyycx_list);

		} else if (flagName.equals("此次随访分类")) {
			int size = sList.size();
			if (size > 0) {
				ccsffl_list = new ArrayList<BD_DD_INFO>();
				for (int i = 0; i < size; i++) {
					String str_id = sList.get(i).getID();
					if (str_id.equals("DM01-71_1") || str_id.equals("DM01-71_2") || str_id.equals("DM01-71_3")
							|| str_id.equals("DM01-71_4") || str_id.equals("")) {
						ccsffl_list.add(sList.get(i));
					}
				}
			}

			nice_spinner_clsffl.attachDataSource(ccsffl_list);
		} else if (flagName.equals("药物单位")) {
			ywdw_list = sList1;
			nice_spinner_dw1.attachDataSource(sList1);
			nice_spinner_dw2.attachDataSource(sList1);
			nice_spinner_dw3.attachDataSource(sList1);
			nice_spinner_dwqt.attachDataSource(sList1);
		}

	}

	@Override
	public void save() {
		// 随诊方式
		String str_id = szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID();
		Log.w(TAG, "随诊方式：" + str_id);
		if (TextUtils.isEmpty(str_id)) {
			nice_spinner_szfs.setBackgroundColor(Color.YELLOW);
			((GXBActivity) getActivity()).showTextDialog("请选择随诊方式");
			return;
		} else {
			nice_spinner_szfs.setBackgroundColor(Color.WHITE);
		}

		// 症状
		String str_zz = adapter_zz.getCheckedIdsStr();
		if (TextUtils.isEmpty(str_zz)) {
			gd_zz.setBackgroundColor(Color.YELLOW);
			((GXBActivity) getActivity()).showTextDialog("请选择症状");
			return;
		} else {
			gd_zz.setBackgroundColor(Color.WHITE);
		}

		// 血压
		String str_xy = ed_xy.getText().toString();
		if (TextUtils.isEmpty(str_xy)) {
			ed_xy.setBackgroundColor(Color.YELLOW);
			((GXBActivity) getActivity()).showTextDialog("请填写血压");
			return;
		} else {
			if (!str_xy.contains("/")) {
				ed_xy.setBackgroundColor(Color.YELLOW);
				((GXBActivity) getActivity()).showTextDialog("血压数据不合理，保存失败");
				return;
			}
			ed_xy.setBackgroundColor(Color.WHITE);
		}

		// 体重
		String str_tz = ed_tz.getText().toString();
		if (TextUtils.isEmpty(str_tz)) {
			ed_tz.setBackgroundColor(Color.YELLOW);
			((GXBActivity) getActivity()).showTextDialog("请输入体重");
			return;
		} else {
			if (Double.valueOf(str_tz) > 300) {
				ed_tz.setBackgroundColor(Color.YELLOW);
				((GXBActivity) getActivity()).showTextDialog("体重最大值为300");
				return;
			}
			ed_tz.setBackgroundColor(Color.WHITE);
		}

		// 下次随访时间
		String str_xcsfsj = ed_xcsfsj.getText().toString();
		if (TextUtils.isEmpty(str_xcsfsj)) {
			ed_xcsfsj.setBackgroundColor(Color.YELLOW);
			((GXBActivity) getActivity()).showTextDialog("请选择下次随访时间");
			return;
		} else {
			ed_xcsfsj.setBackgroundColor(Color.WHITE);
		}
		getRadioButtonCheckedData();
		getCheckBoxedData();
		saveData_followup();
		saveDrug();
	}

	/**
	 * 获取转诊的信息
	 * 
	 * @author 王婷婷
	 * @date 2015年12月16日 下午3:10:48
	 */
	private void getRadioButtonCheckedData() {

		str_dyxyId = adapter_dyxysfjr.getChecked();
		str_xlscId = adapter_xlsc.getChecked();
		str_xjbId = adapter_xjbsfwj.getChecked();
		str_gyId = adapter_zzgy.getChecked();
		str_ywblfyContent = adapter_ywblfy.getCheckedOther();
		str_ywblfyId = adapter_ywblfy.getChecked();

	}

	/**
	 * 获取 被选中的checkbox
	 * 
	 * @author 王婷婷
	 * @date 2015年12月19日 上午11:34:30
	 */
	private void getCheckBoxedData() {

		str_zzId = adapter_zz.getCheckedIdsStr();
		str_zzContent = adapter_zz.getOther();

	}

	/**
	 * 保存数据
	 * 
	 * @author 王婷婷
	 * @date 2015年12月17日 上午11:13:39
	 */
	private void saveData_followup() {

		CD_CHD_FOLLOWUP ques = new CD_CHD_FOLLOWUP();

		ques.setIDCARD(userInfo.getIDCARD());
		// ques.setARCHIVEID(userInfo.getARCHIVEID());// 设置档案编号
		// if (userInfo.getARCHIVEID() == null) {
		// ques.setARCHIVEID("");
		// }
		ques.setARCHIVEID("");
		String str_ed_time1_start = ed_date.getText().toString();
		if (!TextUtils.isEmpty(str_ed_time1_start)) {
			try {
				ques.setFOLLOWUP_DATE((Date) dateFormat.parse(str_ed_time1_start));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		String str_ed_time_next = ed_xcsfsj.getText().toString();
		if (!TextUtils.isEmpty(str_ed_time_next)) {
			try {
				ques.setNEXT_FOLLOWUP_DATE((Date) dateFormat.parse(str_ed_time_next));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		ques.setIS_DEL("N");
		ques.setFOLLOWUP_TYPE(szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID());
		ques.setSYMPTOM(str_zzId);
		ques.setSYMPTOM_EX(str_zzContent);

		String str_xy = ed_xy.getText().toString();
		if (!TextUtils.isEmpty(str_xy)) {
			if (str_xy.contains("/")) {
				ques.setSYSTOLIC(str_xy.split("/")[0]);// 收缩压
				ques.setDIASTOLIC(str_xy.split("/")[1]);// 舒张压
			} else {
				ques.setSYSTOLIC("");// 收缩压
				ques.setDIASTOLIC("");// 舒张压
			}
		} else {
			ques.setSYSTOLIC("");// 收缩压
			ques.setDIASTOLIC("");// 舒张压
		}

		ques.setWEIGHT(ed_tz.getText().toString());

		ques.setHEARTVOICE(str_dyxyId);
		ques.setHEARTRATE(str_xlscId);

		ques.setAPEX(str_xjbId);
		ques.setSMOKING(ed_yj_zysx.getText().toString());
		ques.setSPORTS(ed_yd_zysx.getText().toString());

		ques.setSALT(ed_sy_zysx.getText().toString());

		ques.setACTION(zyxw_list.get(nice_spinner_zyxw.getSelectedIndex()).getID());

		ques.setASSISTCHECK(ed_fzjc.getText().toString());

		ques.setAFTERPILL(str_gyId);
		ques.setCOMPLIANCE(fyycx_list.get(nice_spinner_fyycx.getSelectedIndex()).getID());

		ques.setUNTOWARD(str_ywblfyId);
		ques.setUNTOWARD_EX(str_ywblfyContent);
		ques.setFOLLOW_TYPE(ccsffl_list.get(nice_spinner_clsffl.getSelectedIndex()).getID());
		ques.setREFERRAL_REASON(ed_zzyy.getText().toString());
		ques.setREFERRAL_DEPART(ed_jgkb.getText().toString());
		ques.setFOLLOWUP_DOCTOR(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
		ques.setLAST_UPDATE_DATE(new Date());
		ques.setLAST_UPDATE_BY(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
		if (from_followup == 1) {

			ques.setUPDATEORG(userInfo.getCREATEORG());
			ques.setMODIFIED(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());

			fdbmanger.updata(ques, "IDCARD='" + userInfo.getIDCARD() + "'");
		} else {
			ques.setCREATED_DATE(new Date());
			ques.setCREATEORG(userInfo.getCREATEORG());
			ques.setCREATER(userInfo.getCREATER());
			ques.setCREATED_BY(userInfo.getCREATED_BY());
			fdbmanger.save(ques);
			from_followup = 1;
		}
		((GXBActivity) getActivity()).showTextDialog("保存成功");

	}

	private void saveDrug() {
		CD_DRUGCONDITION drug1 = new CD_DRUGCONDITION();
		drug1.setIDCARD(userInfo.getIDCARD());
		drug1.setTYPE("4");
		drug1.setNAME(ed_yw1.getText().toString());
		drug1.setRES1(ywdw_list.get(nice_spinner_dw1.getSelectedIndex()).getID());
		String str_daytimes = ed_yw1_mr.getText().toString();
		// if (!TextUtils.isEmpty(str_daytimes)) {
		drug1.setDAILYTIME(str_daytimes);
		// }else {
		// drug1.setDAILYTIME(-1);
		// }
		String str_mcjl = ed_yw1_mcjl.getText().toString();
		// if (!TextUtils.isEmpty(str_mcjl)) {
		drug1.setEVERYTIMEMG(str_mcjl);
		// } else {
		// drug1.setEVERYTIMEMG(-1.00);
		// }

		CD_DRUGCONDITION drug2 = new CD_DRUGCONDITION();
		drug2.setIDCARD(userInfo.getIDCARD());
		drug2.setTYPE("4");
		drug2.setNAME(ed_yw2.getText().toString());
		drug2.setRES1(ywdw_list.get(nice_spinner_dw2.getSelectedIndex()).getID());
		String str_daytimes2 = ed_yw2_mr.getText().toString();
		// if (!TextUtils.isEmpty(str_daytimes2)) {
		drug2.setDAILYTIME(str_daytimes2);
		// }else {
		// drug2.setDAILYTIME(-1);
		// }
		String str_mcjl2 = ed_yw2_mcjl.getText().toString();
		// if (!TextUtils.isEmpty(str_mcjl2)) {
		drug2.setEVERYTIMEMG(str_mcjl2);
		// } else {
		// drug2.setEVERYTIMEMG(-1.00);// TODO
		// }

		CD_DRUGCONDITION drug3 = new CD_DRUGCONDITION();
		drug3.setIDCARD(userInfo.getIDCARD());
		drug3.setTYPE("4");
		drug3.setNAME(ed_yw3.getText().toString());
		drug3.setRES1(ywdw_list.get(nice_spinner_dw3.getSelectedIndex()).getID());
		String str_daytimes3 = ed_yw3_mr.getText().toString();
		// if (!TextUtils.isEmpty(str_daytimes3)) {
		drug3.setDAILYTIME(str_daytimes3);
		// }else {
		// drug3.setDAILYTIME(-1);
		// }
		String str_mcjl3 = ed_yw3_mcjl.getText().toString();
		// if (!TextUtils.isEmpty(str_mcjl3)) {
		drug3.setEVERYTIMEMG(str_mcjl3);
		// } else {
		// drug3.setEVERYTIMEMG(-1.00);// TODO
		// }

		CD_DRUGCONDITION drug4 = new CD_DRUGCONDITION();
		drug4.setIDCARD(userInfo.getIDCARD());
		drug4.setTYPE("4");
		drug4.setNAME(ed_yw4.getText().toString());
		drug4.setRES1(ywdw_list.get(nice_spinner_dwqt.getSelectedIndex()).getID());
		String str_daytimes4 = ed_yw4_mr.getText().toString();
		// if (!TextUtils.isEmpty(str_daytimes4)) {
		drug4.setDAILYTIME(str_daytimes4);
		// }else {
		// drug4.setDAILYTIME(-1);
		// }
		String str_mcjl4 = ed_yw4_mcjl.getText().toString();
		// if (!TextUtils.isEmpty(str_mcjl4)) {
		drug4.setEVERYTIMEMG(str_mcjl4);
		// } else {
		// drug4.setEVERYTIMEMG(-1.00);// TODO
		// }

		if (from_drug == 1) {
			if (drug_id1 != -1) {
				fdbmanger.updata(drug1, "IDCARD='" + userInfo.getIDCARD() + "' and type='4' and ID=" + drug_id1);
			} else {
				fdbmanger.save(drug1);
				drug_id1 = 1;
			}
			if (drug_id2 != -1) {
				fdbmanger.updata(drug2, "IDCARD='" + userInfo.getIDCARD() + "' and type='4' and ID=" + drug_id2);
			} else {
				fdbmanger.save(drug2);
				drug_id2 = 1;
			}
			if (drug_id3 != -1) {
				fdbmanger.updata(drug3, "IDCARD='" + userInfo.getIDCARD() + "' and type='4' and ID=" + drug_id3);
			} else {
				fdbmanger.save(drug3);
				drug_id3 = 1;
			}

			if (drug_id4 != -1) {
				fdbmanger.updata(drug4, "IDCARD='" + userInfo.getIDCARD() + "' and type='4' and ID=" + drug_id4);
			} else {
				fdbmanger.save(drug4);
				drug_id4 = 1;
			}

		} else {
			fdbmanger.save(drug1);
			fdbmanger.save(drug2);
			fdbmanger.save(drug3);
			fdbmanger.save(drug4);
		}

	}

	/**
	 * 设置 显示followup
	 * 
	 * @author 王婷婷
	 * @date 2015年12月24日 下午2:25:11
	 */
	private void setData_followUP() {
		List<CD_CHD_FOLLOWUP> queses = fdbmanger.findAllByWhere(CD_CHD_FOLLOWUP.class,
				"IDCARD='" + userInfo.getIDCARD() + "'");
		int size = queses.size();
		if (size > 0) {
			from_followup = 1;

			ed_xm.setText(userInfo.getCUSTOMERNAME());

			CD_CHD_FOLLOWUP ques1 = queses.get(0);

			if (ques1.getFOLLOWUP_DATE() != null) {
				try {
					ed_date.setText(dateFormat.format(ques1.getFOLLOWUP_DATE()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (ques1.getNEXT_FOLLOWUP_DATE() != null) {
				try {
					ed_xcsfsj.setText(dateFormat.format(ques1.getNEXT_FOLLOWUP_DATE()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			nice_spinner_szfs.setSelectedIndex(getListIndex(ques1.getFOLLOWUP_TYPE(), szfs_list));
			str_zzId = ques1.getSYMPTOM();
			adapter_zz.setChecked(str_zzId);
			str_zzContent = ques1.getSYMPTOM_EX();
			adapter_zz.setOther(str_zzContent);

			if (!TextUtils.isEmpty(ques1.getSYSTOLIC())) {

				String str_sy = ques1.getSYSTOLIC() + "/" + ques1.getDIASTOLIC();
				ed_xy.setText(str_sy);
			}

			ed_tz.setText(ques1.getWEIGHT());
			str_dyxyId = ques1.getHEARTVOICE();
			adapter_dyxysfjr.setChecked(str_dyxyId);
			str_xlscId = ques1.getHEARTRATE();
			adapter_xlsc.setChecked(str_xlscId);

			str_xjbId = ques1.getAPEX();
			adapter_xjbsfwj.setChecked(str_xjbId);

			ed_yj_zysx.setText(ques1.getSMOKING());
			ed_yd_zysx.setText(ques1.getSPORTS());
			ed_sy_zysx.setText(ques1.getSALT());

			nice_spinner_zyxw.setSelectedIndex(getListIndex(ques1.getACTION(), zyxw_list));

			ed_fzjc.setText(ques1.getASSISTCHECK());

			str_gyId = ques1.getAFTERPILL();
			adapter_zzgy.setChecked(str_gyId);

			nice_spinner_fyycx.setSelectedIndex(getListIndex(ques1.getCOMPLIANCE(), fyycx_list));
			str_ywblfyId = ques1.getUNTOWARD();
			str_ywblfyContent = ques1.getUNTOWARD_EX();
			adapter_ywblfy.setChecked(str_ywblfyId, str_ywblfyContent);

			nice_spinner_clsffl.setSelectedIndex(getListIndex(ques1.getFOLLOW_TYPE(), ccsffl_list));
			ed_zzyy.setText(ques1.getREFERRAL_REASON());
			ed_jgkb.setText(ques1.getREFERRAL_DEPART());

			for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
				if (ques1.getFOLLOWUP_DOCTOR().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
					ed_sfys.setSelectedIndex(i);
				}

			}

		} else {
			from_followup = 0;

			ed_xm.setText(userInfo.getCUSTOMERNAME());
			ed_date.setText(dateFormat.format(new Date()));
			for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
				if (userInfo.getCREATED_BY().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
					ed_sfys.setSelectedIndex(i);
				}

			}
		}
	}

	/**
	 * 用药情况
	 * 
	 * @author 王婷婷
	 * @date 2015年12月24日 下午2:25:11
	 */
	private void setData_drug() {
		List<CD_DRUGCONDITION> queses = fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
				"IDCARD='" + userInfo.getIDCARD() + "' and TYPE='4'");
		int size = queses.size();
		if (size > 0) {
			from_drug = 1;
			for (int i = 0; i < size; i++) {
				if (i == 0) {

					CD_DRUGCONDITION ques1 = queses.get(0);
					drug_id1 = ques1.getID();
					ed_yw1.setText(ques1.getNAME());
					ed_yw1_mr.setText(ques1.getDAILYTIME() + "");
					ed_yw1_mcjl.setText(ques1.getEVERYTIMEMG() + "");
					nice_spinner_dw1.setSelectedIndex(getListIndex(ques1.getRES1(), ywdw_list));

				} else if (i == 1) {
					CD_DRUGCONDITION ques1 = queses.get(1);
					drug_id2 = ques1.getID();
					ed_yw2.setText(ques1.getNAME());
					ed_yw2_mr.setText(ques1.getDAILYTIME() + "");
					ed_yw2_mcjl.setText(ques1.getEVERYTIMEMG() + "");
					nice_spinner_dw2.setSelectedIndex(getListIndex(ques1.getRES1(), ywdw_list));

				} else if (i == 2) {
					CD_DRUGCONDITION ques1 = queses.get(2);
					drug_id3 = ques1.getID();
					ed_yw3.setText(ques1.getNAME());
					ed_yw3_mr.setText(ques1.getDAILYTIME() + "");
					ed_yw3_mcjl.setText(ques1.getEVERYTIMEMG() + "");
					nice_spinner_dw3.setSelectedIndex(getListIndex(ques1.getRES1(), ywdw_list));

				} else if (i == 3) {
					CD_DRUGCONDITION ques1 = queses.get(3);
					drug_id4 = ques1.getID();
					ed_yw4.setText(ques1.getNAME());
					ed_yw4_mr.setText(ques1.getDAILYTIME() + "");
					ed_yw4_mcjl.setText(ques1.getEVERYTIMEMG() + "");
					nice_spinner_dwqt.setSelectedIndex(getListIndex(ques1.getRES1(), ywdw_list));

				}

			}

		} else {
			from_drug = 0;
		}
	}

	private int getListIndex(String str, List<BD_DD_INFO> bds) {
		if (str != null && !str.equals("")) {

			for (int i = 0; i < bds.size(); i++) {
				if (str.equals(bds.get(i).getID())) {
					return i;
				}
			}
		}
		return 0;
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getActivity(), ExamHistoryActivity.class);
		int id = v.getId();
		if (id == R.id.btn_choose_xy) {
			intent.putExtra("flag", BluetoothConstant.XUEYA_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestXueya);
		} else if (id == R.id.btn_choose_tz) {
			intent.putExtra("flag", BluetoothConstant.TIZHONG_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestTizhong);
		} else {
		}

	}

	@Override
	public void setTizhong(String value) {
		ed_tz.setText(value);
	}

	@Override
	public void setXueya(String gaoya, String diya, String mailv) {
		ed_xy.setText(gaoya + "/" + diya);
	}
}
